﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PilotProject;
using PilotMVC.Models;

namespace PilotMVC.Controllers
{
    
    public class ActivateTestTokenController : Controller
    {
        //
        // GET: /ActivateTestToken/

        private PilotEntities1 db = new PilotEntities1();

        [Authorize(Roles = "Candidate")]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ValidateToken(String id)
        {
            var ttview = new TestTokenView();

            Guid value;

            try
            {
                value = Guid.Parse(id);
                ttview.TestTokenId = value;  
            }
            catch(FormatException)
            {          
                    ModelState.AddModelError("Enter Token Id: ", "Token is not valid, please re-enter");
                    return View("Index");               
            }
             
            TestToken[] testTokens = db.TestTokens.ToArray();
            TestToken selectedToken= db.TestTokens.Include("Candidate").Where(a=>a.Id.Equals(value)).FirstOrDefault();
             ttview.Candidate = db.People.OfType<Candidate>().Where(a => a.Id.Equals(selectedToken.Candidate.Id)).SingleOrDefault();

             ttview.CandidateId = ttview.Candidate.Id;
             ttview.FirstName = ttview.Candidate.FirstName;
             ttview.LastName = ttview.Candidate.LastName;
             ttview.DateOfBirth = ttview.Candidate.DateOfBirth;
             ttview.ARN = ttview.Candidate.ARN;
             ttview.JPA = ttview.Candidate.JPA;
             ttview.Mobile = ttview.Candidate.Mobile;
             ttview.Telephone = ttview.Candidate.Telephone;
             ttview.Email = ttview.Candidate.Email;
             ttview.Address = ttview.Candidate.Address;
         

          

            if (testTokens.Contains(selectedToken))
            {
                if (selectedToken.HasTest)
                {
                    if (selectedToken.Test.HasBeenCompleted)
                    {
                        ModelState.AddModelError("Enter Token : ", "This Token has expired, please re-enter a valid token");
                        return View("Index");
                    }
                    else
                    {
                        return View(ttview);
                    }
                }
                else
                {

                    return View(ttview);
                }
            }
            else 
            {
                ModelState.AddModelError("Enter Token : ", "Token is not valid, please re-enter");
                return View("Index");
            }
      
        }

        //
        // POST: /Candiate/Edit/5
        [HttpPost]
        public ActionResult ValidateToken(TestTokenView ttv,Guid TestTokenId)
        {
            ModelState.Remove("States");
            if (ModelState.IsValid)
            {
                var person = db.People.Where(p => p.Id == ttv.CandidateId).First();
               
                person.Address.SuburbId = ttv.Address.SuburbId;
                person.Address.Street1 = ttv.Address.Street1;
                person.Address.Street2 = ttv.Address.Street2;
                person.Email = ttv.Email;
                person.FirstName = ttv.FirstName;
                person.DateOfBirth = ttv.DateOfBirth;
                person.LastName = ttv.LastName;
                person.Telephone = ttv.Telephone;
                person.Mobile = ttv.Mobile;
                person.ARN = ttv.ARN;
                person.JPA = ttv.JPA;
                db.SaveChanges();
                return RedirectToAction("Load", "Quiz", new { id = TestTokenId });
            }
           // ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Street1", candidate.AddressId);
            var testtoken= db.TestTokens.Where(i => i.Id.Equals(TestTokenId)).FirstOrDefault();
            return View(ttv);
        }
                    
    }
}


